﻿<%@ Application Inherits="System.Web.HttpApplication" Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Web.Http" %>
<%@ Import Namespace="System.Web.Http.WebHost" %>
<%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Common.Configuration" %>
<%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.PolicyInjection" %>
<%@ Import Namespace="WinnerCMS.BLL" %>
<%@ Import Namespace="WinnerCMS.Common" %>
<%@ Import Namespace="WinnerCMS.PlugIn.FileWatcher" %>
<%@ Import Namespace="WinnerCMS.WebUI" %>
<script runat="server">
    protected void Application_Start(Object sender, EventArgs e)
    {
        var injector = new PolicyInjector(ConfigurationSourceFactory.Create());
        PolicyInjection.SetPolicyInjector(injector);

        //BundleTable.EnableOptimizations = true;

        //ViewEngines.Engines.Clear();
        //ViewEngines.Engines.Insert(0, new TemplateViewEngine("WinnerCMS6"));

        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        BootstrapBundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.EnsureInitialized();


        new Thread(() =>
        {
            if (CMSConfig.Instance().FileWatcher.StartupFileWatch)
            {
                var fs = new FileWatchScan();
                fs.Watch();
            }
        }).Start();
    }

    protected void Session_Start(Object sender, EventArgs e)
    {
    }


    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    }

    protected void Application_EndRequest(Object sender, EventArgs e)
    {
    }

    protected void Application_End(Object sender, EventArgs e)
    {
    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
    }

    protected void Application_PostAuthorizeRequest()
    {
        // WebApi SessionState
        var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
        if (routeData != null && routeData.RouteHandler is HttpControllerRouteHandler)
            HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
    }

    protected void Application_Error(Object sender, EventArgs e)
    {
        var lastError = Server.GetLastError().InnerException ?? Server.GetLastError();

        LogHelper.Write(lastError, LogCategory.General, Priority.High, TraceEventType.Error);
        //清空异常
        //Server.ClearError();
    }

    protected void Session_End(Object sender, EventArgs e)
    {
    }
</script>